﻿using System;
using System.Collections.Generic;
using System.Web;
using Kratos.Framework.ContentManagement.Models.SystemRights;
using Kratos.Framework.Extensions;
using Kratos.Framework.Infrastructures;
using Kratos.Framework.Modules.Core.Repositories;
using Kratos.Framework.Repositories;
using System.Threading;
using Kratos.Framework.ContentManagement.ViewModels;
using System.Linq;

namespace Kratos.Framework.Modules.Core.Services.Impl
{
    public class UserInfoServices : IUserInfoServices
    {
        readonly IRepository<UserInfo> _repository;
        readonly IFormsAuthentication _formsAuth;

        public UserInfoServices(IRepository<UserInfo> repository, IFormsAuthentication formsAuth)
        {
            _repository = repository;
            _formsAuth = formsAuth;
        }

        public void Save(UserInfo userInfo)
        {
            if (!userInfo.LastLogOnTime.HasValue)
                userInfo.LastLogOnTime = DateTime.Now;
            if (!userInfo.CreateTime.HasValue)
                userInfo.CreateTime = DateTime.Now;

            _repository.SaveOrUpdate(userInfo);
            //Thread.CurrentPrincipal = HttpContext.Current.User = userInfo;
        }

        public bool IsExistUser(string userId)
        {
            return _repository.GetAll().IsExistUser(userId);
        }

        public void SetAuthenticationCookie(string username)
        {
            _formsAuth.SetAuthCookie(username, true);
        }

        public void SignOut()
        {
            _formsAuth.SignOut();
        }

        public string HashPassword(string password)
        {
            return _formsAuth.HashPassword(password);
        }

        public UserInfo CurrentUser
        {
            get
            {
                return HttpContext.Current.User as UserInfo;
            }
        }

        public bool CanLogOn(LogModel model)
        {
            if (CurrentUser != null && CurrentUser.Identity.IsAuthenticated) return false;

            var user = _repository.GetAll().GetUser(model.UserId);
            bool val = user != null && HashPassword(model.Password).Equals(user.Password);
            if (val)
            {
                Thread.CurrentPrincipal = HttpContext.Current.User = user;
            }
            return val;
        }

        public UserInfo GetUser(string userId)
        {
            return _repository.GetAll().GetUser(userId);
        }

        public IEnumerable<UserInfo> GetUserWithoutGroup(Group group, int pageIndex, int pageSize)
        {
            return _repository.GetAll().GetUserWithoutGroup(group).ToPageList(pageIndex, pageSize);
        }

        public IList<UserInfo> GetUsersByIdJoinString(string userIds)
        {
            var idArray = userIds.Split(',');
            //貌似Nhibernate不支持在Linq中使用ToString方法,这里不得不进行拆箱操作，寻求解决方法
            int[] ids = Array.ConvertAll<string, int>(idArray, int.Parse);

            return _repository.GetAll().Where(u => ids.Contains(u.Id)).ToList();
        }
    }
}
